package ar.edu.itba.pod.legajo47573;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import org.joda.time.Duration;

import ar.edu.itba.node.NodeInformation;
import ar.edu.itba.pod.agent.market.Consumer;
import ar.edu.itba.pod.agent.market.Producer;
import ar.edu.itba.pod.agent.market.Resource;
import ar.edu.itba.pod.time.TimeMapper;
import ar.edu.itba.pod.time.TimeMappers;

public class MySimulationApp implements Runnable {
	
	private static String id;
	private static String host;
	private static int port;
	private static String remoteHost;
	private static int remotePort;
	
	public static void main(String[] args) {
		System.out.println("empiezo");
		id = args[0];
		host = args[1];
		port = Integer.valueOf(args[2]);
		if(!id.equals("first")){
			remoteHost = args[3];
			remotePort = Integer.valueOf(args[4]);
		}
		new MySimulationApp().run();
	}

	@Override
	public void run() {

		NodeInformation info = new NodeInformation(host, port, id);
		TimeMapper timeMapper = TimeMappers.oneSecondEach(Duration.standardHours(6));
		
		MyNode node = new MyNode(info/*, timeMapper*/);

		Resource steel = new Resource("Alloy", "Steel");

		int i = 1;
		
		if(id.equals("first")){
			try {
				node.getCluster().createGroup();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
			//node.getSimulation().add(new Producer("steel mine" + i, steel, Duration.standardDays(1), 5));
		}
		else{
			try {
				System.out.println("conectando");
				node.getCluster().connectToGroup(remoteHost, remotePort);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//node.getSimulation().add(new Consumer("steel consumer" + i, steel, Duration.standardDays(3), 2));
		}
		
		System.out.println("Starting ...");
/*		try {
			node.getSimulation().startAndWait(Duration.standardMinutes(10));
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("Ending ...");*/
//		System.out.println("Total transactions: " + Iterables.size(logger.transferItems()));
	}
}
